clear
clear all
cap log close

log using "log-files/2_Figure_A1.smcl", replace smcl

timer on 2

*------------*
* Figure A.1 *
*------------*
	* Figure A.1.a
	*-------------
		use "data/outputs/computo_ulttranstrepdate_nomissings.dta", clear

		keep NumMesa trep_date_ulttrans pcs_so
		rename trep_date_ulttrans trep_date_ult_trans_date
		rename pcs_so pcs_so_ult_trans_date

		tempfile ult
		save `ult'

		* use data set with problem booths appended at the end
		use "data/outputs/computo_verificadortrepdate_nomissings.dta", clear
		keep NumMesa trep_date_verificador pcs_so trep_missing_date

		* merge transmission times
		merge 1:1 NumMesa using `ult'
		drop _merge
		sort trep_date_verificador

		* choose dates to label on x axis
		local x1 = clock("20oct2019 04:30:00","DMY hms")
		local x2 = clock("20oct2019 16:30:00","DMY hms")
		local x3 = clock("20oct2019 21:00:00","DMY hms")
		local x4 = clock("21oct2019 14:51:00","DMY hms")
		local x5 = clock("22oct2019 18:54:00","DMY hms")

		drop pcs* trep_missing_date_verificador

		* cdf, verified
		count if trep_date_verificador != .
		local nomiss = r(N)
		sort trep_date_verificador
		gen pcs_so = _n / `nomiss' if trep_date_verificador != .

		* cdf, transmitted
		count if trep_date_ult_trans_date != .
		local nomiss = r(N)
		sort trep_date_ult_trans_date
		gen pcs_so_ult = _n / `nomiss' if trep_date_ult_trans_date != .

		* empirical densities
		twoway (kdensity trep_date_verificador, lcolor(gs6) lwidth(thick)) ///
			(kdensity trep_date_ult_trans_date, lcolor(midblue) lwidth(thick)), ///
			graphregion(color(white)) ylabel(, format(%12.1e) angle(0) labsize(small)) ///
			xlabel(`x1' `""10/20" "4:30am""' `x2' `""10/20" "4:30" "pm""' ///
			`x3' `""10/20" "9pm""' `x4' `""10/21" "2:51pm""' ///
			`x5' `""10/22" "6:54pm""', format(%tc) labsize(vsmall)) ///
			xtitle("Date and Time") legend(lab(1 "Verification Time") ///
				lab(2 "Transmission Time") order(2 1) cols(1) pos(1) ring(0))
		graph export "outputs/generated/Figure_A1a.pdf", replace

	* Figure A.1.b
	*-------------
		twoway (line pcs_so trep_date_verificador, ///
					lcolor(gs6) lwidth(thick) sort) ///
			(scatter pcs_so_ult trep_date_ult_trans_date, ///
				mcolor(midblue) msize(small)), ///
			graphregion(color(white)) ///
			ytitle("Percent of booths counted") ///
			xtitle("Date and Time") ///
			ylab(, angle(0)) ///
			xlabel(`x1' `""10/20" "4:30am""' `x2' `""10/20" "4:30" "pm""' ///
			`x3' `""10/20" "9pm""' `x4' `""10/21" "2:51pm""' ///
			`x5' `""10/22" "6:54pm""', format(%tc) labsize(vsmall)) ///
			legend(pos(4) ring(0) rows(2) order(2 "Transmission Time" ///
				1 "Verification Time"))
		graph export "outputs/generated/Figure_A1b.png", replace

*------------------------------------------------------------------------------*
timer off 2
timer list 2

log close
clear all
